home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / libpng / pngmem.c < prev    next >
C/C++ Source or Header  |  1996-06-17  |  10KB  |  386 lines

  1.  
  2. /* pngmem.c - stub functions for memory allocation
  3.  
  4.    libpng 1.0 beta 3 - version 0.89
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    May 25, 1996
  8.  
  9.    This file provides a location for all memory allocation.  Users which
  10.    need special memory handling are expected to modify the code in this file
  11.    to meet their needs.  See the instructions at each function. */
  12.  
  13. #define PNG_INTERNAL
  14. #include "png.h"
  15.  
  16. /* Borland DOS special memory handler */
  17. #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
  18. /* if you change this, be sure to change the one in png.h also */
  19.  
  20. /* Allocate memory for a png_struct.  The malloc and memset can be replaced
  21.  * by a single call to calloc() if this is thought to improve performance.
  22.  */
  23. png_voidp
  24. png_create_struct(uInt type)
  25. {
  26.    png_size_t type;
  27.    png_voidp struct_ptr;
  28.  
  29.    if (type == PNG_STRUCT_INFO)
  30.      size = sizeof(png_info);
  31.    else if (type == PNG_STRUCT_PNG)
  32.      size = sizeof(png_struct);
  33.    else
  34.      return (png_voidp)NULL;
  35.  
  36.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  37.    {
  38.       png_memset(struct_ptr, 0, size);
  39.    }
  40.  
  41.    return (struct_ptr);
  42. }
  43.  
  44.  
  45. /* Free memory allocated by a png_create_struct() call */
  46. void
  47. png_destroy_struct(png_voidp struct_ptr)
  48. {
  49.    if (struct_ptr)
  50.       farfree (struct_ptr);
  51. }
  52.  
  53. /* Allocate memory.  For reasonable files, size should never exceed
  54.    64K.  However, zlib may allocate more then 64K if you don't tell
  55.    it not to.  See zconf.h and png.h for more information. zlib does
  56.    need to allocate exactly 64K, so whatever you call here must
  57.    have the ability to do that. */
  58.  
  59. /* Borland seems to have a problem in DOS mode for exactly 64K.
  60.    It gives you a segment with an offset of 8 (perhaps to store it's
  61.    memory stuff).  zlib doesn't like this at all, so we have to
  62.    detect and deal with it.  This code should not be needed in
  63.    Windows or OS/2 modes, and only in 16 bit mode.  This code has
  64.    been updated by Alexander Lehmann for version 0.89 to waste less
  65.    memory.
  66. */
  67.  
  68. png_voidp
  69. png_large_malloc(png_structp png_ptr, png_uint_32 size)
  70. {
  71.    png_voidp ret;
  72.    if (!png_ptr || !size)
  73.       return ((voidp)NULL);
  74.  
  75. #ifdef PNG_MAX_MALLOC_64K
  76.    if (size > (png_uint_32)65536L)
  77.       png_error(png_ptr, "Cannot Allocate > 64K");
  78. #endif
  79.  
  80.    if (size == (png_uint_32)(65536L))
  81.    {
  82.       if (!png_ptr->offset_table)
  83.       {
  84.          /* try to see if we need to do any of this fancy stuff */
  85.          ret = farmalloc(size);
  86.          if (!ret || ((long)ret & 0xffff))
  87.          {
  88.             int num_blocks;
  89.             png_uint_32 total_size;
  90.             png_bytep table;
  91.             int i;
  92.             png_byte huge * hptr;
  93.  
  94.             if (ret)
  95.                farfree(ret);
  96.             ret = NULL;
  97.  
  98.             num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  99.             if (num_blocks < 1)
  100.                num_blocks = 1;
  101.             if (png_ptr->zlib_mem_level >= 7)
  102.                num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  103.             else
  104.                num_blocks++;
  105.  
  106.             total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  107.  
  108.             table = farmalloc(total_size);
  109.  
  110.             if (!table)
  111.             {
  112.                png_error(png_ptr, "Out of Memory");
  113.             }
  114.  
  115.             if ((long)table & 0xfff0)
  116.             {
  117.                png_error(png_ptr, "Farmalloc didn't return normalized pointer");
  118.             }
  119.  
  120.             png_ptr->offset_table = table;
  121.             png_ptr->offset_table_ptr = farmalloc(
  122.                num_blocks * sizeof (png_bytep));
  123.  
  124.             if (!png_ptr->offset_table_ptr)
  125.             {
  126.                png_error(png_ptr, "Out of memory");
  127.             }
  128.  
  129.             hptr = (png_byte huge *)table;
  130.             if ((long)hptr & 0xf)
  131.             {
  132.                hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  133.                hptr += 16L;
  134.             }
  135.             for (i = 0; i < num_blocks; i++)
  136.             {
  137.                png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  138.                hptr += 65536L;
  139.             }
  140.  
  141.             png_ptr->offset_table_number = num_blocks;
  142.             png_ptr->offset_table_count = 0;
  143.             png_ptr->offset_table_count_free = 0;
  144.          }
  145.       }
  146.  
  147.       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  148.          png_error(png_ptr, "Out of Memory");
  149.  
  150.       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  151.    }
  152.    else
  153.       ret = farmalloc(size);
  154.  
  155.    if (ret == NULL)
  156.    {
  157.       png_error(png_ptr, "Out of Memory");
  158.    }
  159.  
  160.    return ret;
  161. }
  162.  
  163. /* free a pointer allocated by png_large_malloc().  In the default
  164.   configuration, png_ptr is not used, but is passed in case it
  165.   is needed.  If ptr is NULL, return without taking any action. */
  166. void
  167. png_large_free(png_structp png_ptr, png_voidp ptr)
  168. {
  169.    if (!png_ptr)
  170.       return;
  171.  
  172.    if (ptr != NULL)
  173.    {
  174.       if (png_ptr->offset_table)
  175.       {
  176.          int i;
  177.  
  178.          for (i = 0; i < png_ptr->offset_table_count; i++)
  179.          {
  180.             if (ptr == png_ptr->offset_table_ptr[i])
  181.             {
  182.                ptr = 0;
  183.                png_ptr->offset_table_count_free++;
  184.                break;
  185.             }
  186.          }
  187.          if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  188.          {
  189.             farfree(png_ptr->offset_table);
  190.             farfree(png_ptr->offset_table_ptr);
  191.             png_ptr->offset_table = 0;
  192.             png_ptr->offset_table_ptr = 0;
  193.          }
  194.       }
  195.  
  196.       if (ptr)
  197.          farfree(ptr);
  198.    }
  199. }
  200.  
  201. #else /* Not the Borland DOS special memory handler */
  202.  
  203. /* Allocate memory for a png_struct or a png_info.  The malloc and
  204.  * memset can be replaced by a single call to calloc() if this is thought
  205.  * to improve performance noticably.
  206.  */
  207. png_voidp
  208. png_create_struct(uInt type)
  209. {
  210.    size_t size;
  211.    png_voidp struct_ptr;
  212.  
  213.    if (type == PNG_STRUCT_INFO)
  214.      size = sizeof(png_info);
  215.    else if (type == PNG_STRUCT_PNG)
  216.      size = sizeof(png_struct);
  217.    else
  218.      return (png_voidp)NULL;
  219.  
  220. #if defined(__TURBOC__) && !defined(__FLAT__)
  221.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  222. #else
  223. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  224.    if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
  225. # else
  226.    if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
  227. # endif
  228. #endif
  229.    {
  230.       png_memset(struct_ptr, 0, size);
  231.    }
  232.  
  233.    return (struct_ptr);
  234. }
  235.  
  236.  
  237. /* Free memory allocated by a png_create_struct() call */
  238. void
  239. png_destroy_struct(png_voidp struct_ptr)
  240. {
  241.    if (struct_ptr)
  242. #if defined(__TURBOC__) && !defined(__FLAT__)
  243.       farfree(struct_ptr);
  244. #else
  245. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  246.       hfree(struct_ptr);
  247. # else
  248.       free(struct_ptr);
  249. # endif
  250. #endif
  251. }
  252.  
  253.  
  254. /* Allocate memory.  For reasonable files, size should never exceed
  255.    64K.  However, zlib may allocate more then 64K if you don't tell
  256.    it not to.  See zconf.h and png.h for more information. zlib does
  257.    need to allocate exactly 64K, so whatever you call here must
  258.    have the ability to do that. */
  259.  
  260. png_voidp
  261. png_large_malloc(png_structp png_ptr, png_uint_32 size)
  262. {
  263.    png_voidp ret;
  264.    if (!png_ptr || !size)
  265.       return ((voidp)0);
  266.  
  267. #ifdef PNG_MAX_MALLOC_64K
  268.    if (size > (png_uint_32)65536L)
  269.       png_error(png_ptr, "Cannot Allocate > 64K");
  270. #endif
  271.  
  272. #if defined(__TURBOC__) && !defined(__FLAT__)
  273.    ret = farmalloc(size);
  274. #else
  275. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  276.    ret = halloc(size, 1);
  277. # else
  278.    ret = malloc(size);
  279. # endif
  280. #endif
  281.  
  282.    if (ret == NULL)
  283.    {
  284.       png_error(png_ptr, "Out of Memory");
  285.    }
  286.  
  287.    return ret;
  288. }
  289.  
  290. /* free a pointer allocated by png_large_malloc().  In the default
  291.   configuration, png_ptr is not used, but is passed in case it
  292.   is needed.  If ptr is NULL, return without taking any action. */
  293. void
  294. png_large_free(png_structp png_ptr, png_voidp ptr)
  295. {
  296.    if (!png_ptr)
  297.       return;
  298.  
  299.    if (ptr != NULL)
  300.    {
  301. #if defined(__TURBOC__) && !defined(__FLAT__)
  302.       farfree(ptr);
  303. #else
  304. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  305.       hfree(ptr);
  306. # else
  307.       free(ptr);
  308. # endif
  309. #endif
  310.    }
  311. }
  312.  
  313. #endif /* Not Borland DOS special memory handler */
  314.  
  315. /* Allocate memory.  This is called for smallish blocks only  It
  316.    should not get anywhere near 64K.  On segmented machines, this
  317.    must come from the local heap (for zlib).  Currently, zlib is
  318.    the only one that uses this, so you should only get one call
  319.    to this, and that a small block. */
  320. void *
  321. png_malloc(png_structp png_ptr, png_uint_32 size)
  322. {
  323.    void *ret;
  324.  
  325.    if (!png_ptr || !size)
  326.    {
  327.       return ((void *)0);
  328.    }
  329.  
  330. #ifdef PNG_MAX_MALLOC_64K
  331.    if (size > (png_uint_32)65536L)
  332.       png_error(png_ptr, "Cannot Allocate > 64K");
  333. #endif
  334.  
  335.  
  336.    ret = malloc((png_size_t)size);
  337.  
  338.    if (!ret)
  339.    {
  340.       png_error(png_ptr, "Out of Memory");
  341.    }
  342.  
  343.    return ret;
  344. }
  345.  
  346. /* Reallocate memory.  This will not get near 64K on a
  347.    even marginally reasonable file.  This is not used in
  348.    the current version of the library. */
  349. void *
  350. png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size,
  351.    png_uint_32 old_size)
  352. {
  353.    void *ret;
  354.  
  355.    if (!png_ptr || !old_size || !ptr || !size)
  356.       return ((void *)0);
  357.  
  358. #ifdef PNG_MAX_MALLOC_64K
  359.    if (size > (png_uint_32)65536L)
  360.       png_error(png_ptr, "Cannot Allocate > 64K");
  361. #endif
  362.  
  363.    ret = realloc(ptr, (png_size_t)size);
  364.  
  365.    if (!ret)
  366.    {
  367.       png_error(png_ptr, "Out of Memory 7");
  368.    }
  369.  
  370.    return ret;
  371. }
  372.  
  373. /* free a pointer allocated by png_malloc().  In the default
  374.   configuration, png_ptr is not used, but is passed incase it
  375.   is needed.  If ptr is NULL, return without taking any action. */
  376. void
  377. png_free(png_structp png_ptr, void * ptr)
  378. {
  379.    if (!png_ptr)
  380.       return;
  381.  
  382.    if (ptr != (void *)0)
  383.       free(ptr);
  384. }
  385.  
  386.